package com.example.minicat.server;

import java.io.*;

public class MyClassLoader extends ClassLoader{
    private String path;

    public MyClassLoader(String path) {
        this.path = path;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {

        Class log =null;
        byte[] classData = getData();
        if (classData != null){
            log = defineClass(name,classData,0,classData.length);
        }
        return log;
    }

    private byte[] getData() {
        File file = new File(path);
        if (file.exists()){
            FileInputStream in = null;
            ByteArrayOutputStream out = null;
            try{
                in = new FileInputStream(file);
                out = new ByteArrayOutputStream();

                byte[] buffer = new byte[1024];
                int size = 0;
                while ((size = in.read(buffer)) != -1){
                    out.write(buffer,0,size);
                }
            }catch (IOException e){
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return out.toByteArray();
        }
        return null;
    }
}
